XPath Advanced Functions Examples
The following examples will be based on the following data model. Imagine you work for a telephone company and you are in charge of the Help Desk to assist with claims and complaints. Each claim will be related to a customer. To resolve each case, your team may contact the customer by phone several times. To keep track of all calls made, a History log will be constantly updated.
newCollectionItem
Allows you to include new records in the collection. The records are initially added as an empty record. You can use XPath or variables to set the new record values.
Example:
Each comment must be registered in the History collection.
var User = Me.Case.WorkingCredential.UserId;
CommentRecord = Me.newCollectionItem("ClaimsAndComplaints.History");
CommentRecord.setXPath("Actions", <ClaimsAndComplaints.Response>);
CommentRecord.setXPath("PostedBy", User);
CommentRecord.setXPath("Dates", DateTime.Now);
Distinct-values
Returns a filtered collection with no repeated values of an attribute. It is necessary to indicate the attribute for which the values will not be repeated.
Example: We need to know how many different phone numbers have been dialed to contact the customer.
DiffPhone = <distinct-values(ClaimsAndComplaints.Calls.TelephoneNumber)>;
<ClaimsAndComplaints.DiffPhoneNumbers> = DiffPhone.size();
Get value as collection
In some cases, it is necessary to handle collections in expressions without using the "Iterate over XPath" functionality to allow a complete manipulation of the code.
Example: It is necessary that all calls have an observation. The function transforms a list of all calls stored as a text chain into an array list called AllCalls. Note: This example is used to illustrate the functionality; it could be done using "Iterate over XPath."
var AllCalls = CHelper.GetValueAsCollection(<ClaimsAndComplaints.Calls>);
for (var i = 0; i < AllCalls.size(); i++) {
var CallRecord = AllCalls.get(i);
if (BAIsNull(CallRecord.getXPath("Observations")))
CHelper.ThrowValidationError("The Call " + CallRecord.getXPath("TelephoneNumber") + " has no observations");
}
Note: The XPath should NOT contain the parent entity object—only the collection within the entity.
RemoveRelation
Allows you to delete items from a collection. It is possible to specify whether you want or not to delete the database records in the second parameter.
Example: All the calls that have not been marked must be removed.
Me.removeRelation("ClaimsAndComplaints.Calls[Mark = false]", true);
Sort
Sorts records of a collection in ascending order according to an attribute. It is possible to sort using an integer, float, date, or string attribute. The method receives a collection and returns the collection sorted by the attribute’s value.
Example: We will sort by date all the reviewed Calls and store them in a variable.
var CallsChecked = <sort(ClaimsAndComplaints.Calls[Mark = false], 'DateAndTime')>;
Round
Used to reduce a number (currency, real, float) given a specific number of decimals.
Example: Round a currency value with two decimal places.
<Purchase.RoundedPrice> = CHelper.Math.Round(<Purchase.InitialPrice>, 2);
Round a currency value without decimal places.
<Purchase.RoundedPrice> = CHelper.Math.Round(<Purchase.InitialPrice>);
Floor
Used to round a number (currency, real, float) down to the closest integer.
Example: Round a currency attribute to the smallest integer.
<Purchase.RoundedPrice> = CHelper.Math.Floor(<Purchase.InitialPrice>);
Ceiling
Used to round a number (currency, real, float) up to the closest integer.
Example: Round a currency attribute to the biggest integer.
<Purchase.RoundedPrice> = CHelper.Math